package com.practice.niuke.new_direct_basics.class08;

/**
 * 汉诺塔问题:
 * 打印n层汉诺塔从最左边移动到最右边的全部过程
 */
public class Code01_Hanoi {

    /**
     * 优化后，N层汉诺塔问题
     * @param n
     */
    public static void hanoi(int n) {
        if (n > 0) {
            func(n, "左", "右", "中");
        }
    }

    /**
     * 优化后，N层汉诺塔问题，从from到to的过程，other为中间辅助
     * (1~i 圆盘 目标是from -> to， other是另外一个)
     *
     * @param N
     * @param from
     * @param to
     * @param other
     */
    public static void func(int N, String from, String to, String other) {
        if (N == 1) { // base
            System.out.println("Move 1 from " + from + " to " + to);
        } else {
            func(N - 1, from, other, to);
            System.out.println("Move " + N + " from " + from + " to " + to);
            func(N - 1, other, to, from);
        }
    }

    /**
     * N层汉诺塔问题（TODO：标记主函数）
     *
     * @param N
     */
    public static void all(int N) {
        LeftToRight(N);
    }

    /**
     * N层汉诺塔问题从左到右的过程
     *
     * @param N
     */
    public static void LeftToRight(int N) {
        if (N == 1) {
            System.out.println("Move 1 from Left to Right.");
            return;
        } else {// N > 1
            LeftToMid(N - 1);
            System.out.println("Move" + N + "from Left to Right.");
            MidToRight(N - 1);
        }
    }

    /**
     * N层汉诺塔问题从左到中的过程
     *
     * @param N
     */
    public static void LeftToMid(int N) {
        if (N == 1) {
            System.out.println("Move 1 from Left to Mid.");
            return;
        } else {// N > 1
            LeftToRight(N - 1);
            System.out.println("Move" + N + "from Left to Mid.");
            RightToMid(N - 1);
        }
    }

    /**
     * N层汉诺塔问题从中到右的过程
     *
     * @param N
     */
    public static void MidToRight(int N) {
        if (N == 1) {
            System.out.println("Move 1 from Mid to Right.");
            return;
        } else {// N > 1
            MidToLeft(N - 1);
            System.out.println("Move" + N + "from Mid to Right.");
            LeftToRight(N - 1);
        }
    }

    /**
     * N层汉诺塔问题从中到左的过程
     *
     * @param N
     */
    public static void MidToLeft(int N) {
        if (N == 1) {
            System.out.println("Move 1 from Mid to Left.");
            return;
        } else {// N > 1
            MidToRight(N - 1);
            System.out.println("Move" + N + "from Mid to Left.");
            RightToLeft(N - 1);
        }
    }

    /**
     * N层汉诺塔问题从右到左的过程
     *
     * @param N
     */
    public static void RightToLeft(int N) {
        if (N == 1) {
            System.out.println("Move 1 from Right to Left.");
            return;
        } else {// N > 1
            RightToMid(N - 1);
            System.out.println("Move" + N + "from Right to Left.");
            MidToLeft(N - 1);
        }
    }

    /**
     * N层汉诺塔问题从右到中的过程
     *
     * @param N
     */
    public static void RightToMid(int N) {
        if (N == 1) {
            System.out.println("Move 1 from Right to Mid.");
            return;
        } else {// N > 1
            RightToLeft(N - 1);
            System.out.println("Move" + N + "Right to Mid.");
            LeftToMid(N - 1);
        }
    }


    public static void main(String[] args) {
        int n = 3;
        hanoi(n);
    }

}
